/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Mar 20, 2007
 * 
 */

package org.biojava.services.das.registry;

import java.net.MalformedURLException;
import java.net.URL;

import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
import org.biojava.dasobert.dasregistry.DasSource;


public class AutoActivation {

	DasClient client;
	DasCoordinateSystem coordSys;
	String activationURL;
	
	/** a bean to manage the information about auto - activation URLS in DAS clients
	 * 
	 *
	 */
	public AutoActivation(){
		
	}
	
	public String toString(){
		return "AutoActivation for client " + client.getName() + " " + coordSys.toString();
	}

	/** get the URL for auto activation.
	 *  it can contain the following 
	 *  placeholder that will be filled up with the valid data in getURL(DasSource);
	 * 
	 * %id will be replaced with the unique ID of a DAS source 
	 * %url will be replaced with the url of a DAS source 
	 * %nickname will be replaced with the nickname of a DAS source 
	 * %testcode will be replaced with the testcode of a DAS source
	 *
	 * a location like 2R:500,1000 contains
	 * reference: 2R start: 500 stop: 1000 
	 * %reference only for chromosomal coordinates - the reference of the location 
	 * %start only for chromosomal coordinates the start of the location 
	 * %stop only for chromosomal coordinates the end of the location 
	 * 
	 * ensembl specific things ...
	 * %ensurl is the url of the server without the last bit (which e! calls dsn - das source name)
	 * %dsn    the last bit of the url 
	 * 
	 * @return the template String for the auto activation
	 */
	public String getActivationURL() {
		return activationURL;
	}

	public void setActivationURL(String activationURL) {
		this.activationURL = activationURL;
	}

	public DasClient getClient() {
		return client;
	}

	public void setClient(DasClient client) {
		this.client = client;
	}

	public DasCoordinateSystem getCoordSys() {
		return coordSys;
	}

	public void setCoordSys(DasCoordinateSystem coordSys) {
		this.coordSys = coordSys;
	}
	
	
	/** get the actual URL for display
	 * see getActivationURL for the actual placeholders that are being used
	 * 
	 * @param ds
	 * @return the valid URL that can be used for auto-activation
	 * @throws MalformedURLException 
	 */
	public URL getURL(DasSource ds) throws MalformedURLException{
		
			String activationString = getActivationURL();

			String testCode = getCoordSys().getTestCode();

			String reference = "";
			String start = "";
			String stop  = "";
			String[] spl = testCode.split(":");
			if (spl.length <2) {
				reference = testCode;
			} else if ( spl.length ==2) {
				reference = spl[0];
				String coordsspl = spl[1];
				String[] splc = coordsspl.split(",");
				if ( splc.length == 2 ) {
					start = splc[0];
					stop  = splc[1];
				}
			}

			String nickname  = ds.getNickname().replaceAll(" ","_");
			String url       = ds.getUrl();

//			e! specific stuff;
			String[] urlsplit = url.split("/");
			String dsn = urlsplit[urlsplit.length-1];

			String ensurl = "";
			for ( int i=0;i<urlsplit.length-1;i++) {
				ensurl+=urlsplit[i];
				if ( i < urlsplit.length-2)
					ensurl += "/";
			}


			activationString = activationString.replaceAll("%id",ds.getId());
			activationString = activationString.replaceAll("%testcode",testCode);
			activationString = activationString.replaceAll("%nickname",nickname);
			activationString = activationString.replaceAll("%url",url);
			activationString = activationString.replaceAll("%reference",reference);
			activationString = activationString.replaceAll("%start",start);
			activationString = activationString.replaceAll("%stop",stop);
			activationString = activationString.replaceAll("%ensurl",ensurl);
			activationString = activationString.replaceAll("%dsn",dsn);

			
			URL u = new URL(activationString);
			return u;
	}

	
	
}
